package com.alibaba.citrus.cr.price.ability.functions.entity.activity;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.cr.price.ability.functions.constant.ActivityOrderTagEnum;
import com.alibaba.citrus.cr.price.repository.ActivityQueryRepository;
import com.alibaba.citrus.cr.price.repository.ActivityWriteRepository;
import com.alibaba.citrus.cr.common.annotation.FunctionInvoker;
import com.alibaba.citrus.cr.common.annotation.ProcessInvoker;
import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.common.exception.ProcessException;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.MapUtils;
import com.epoch.app.crpricecenter.dto.request.*;
import com.epoch.app.crpricecenter.dto.response.*;
import com.epoch.app.crpricecenter.model.enums.ActivityTypeEnum;
import com.epoch.app.crpricecenter.sdo.ActivityUseRecordSDO;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: qiaojiange
 * @create: 2021/10/17
 * @Description 活动抬头实体
 */
@Data
public class ActivityEntity {
    private final static ArrayList<String> replenishList = Lists.newArrayList(ActivityTypeEnum.REPLENISH_LIST.getValue().split(","));

    Log log = Log.getLogger(ActivityEntity.class);


    /**
     * 活动抬头实体
     */
    private ActivityTitleEntity titleEntity;

    /**
     * 活动抬头扩展实体
     */
    private ActivityTitleExtEntity titleExtEntity;

    /**
     * 活动项目实体
     */
    private List<ActivityTermEntity> termEntityList;

    /**
     * 活动使用记录实体
     */
    private ActivityUseRecordRelationEntity useEntity;

    /**
     * 活动写入
     */
    private static ActivityWriteRepository writeRepository = SpringContextUtil.getBean(ActivityWriteRepository.class);

    /**
     * 活动查询
     */
    private static ActivityQueryRepository queryRepository = SpringContextUtil.getBean(ActivityQueryRepository.class);

    /**
     * 活动 term 的快照
     */
    private String ACTIVITY_TERM_SNAPSHOT = "activityTermSnapshot";
    private String ACTIVITY_GIFT_HAVE_FEE = "activityGiftHaveFee";

    /**
     * 构造方法
     *
     * @param titleEntity
     * @param termEntityList
     * @param titleExtEntity
     * @return
     */
    public static ActivityEntity of(ActivityTitleEntity titleEntity,
                                    List<ActivityTermEntity> termEntityList,
                                    ActivityTitleExtEntity titleExtEntity
            , ActivityUseRecordRelationEntity useRecordEntity) {
        ActivityEntity entity = new ActivityEntity();
        entity.setTitleEntity(titleEntity);
        entity.setTitleExtEntity(titleExtEntity);
        entity.setTermEntityList(termEntityList);
        entity.setUseEntity(useRecordEntity);
        return entity;
    }

    public static void setFailData(List<CoverActivityInfoRequest> notCoverActivityInfos, CoverInActivityResponse coverInActivityResponse) {

        List<CoverActivityInfoResponse> collect = notCoverActivityInfos.stream().map(item -> {
            CoverActivityInfoResponse response = new CoverActivityInfoResponse();
            //活动id
            response.setActivityId(item.getActivityId());

//        主单号
            response.setMainOrderId(item.getMainOrderId());
            /**
             * 订单类型	必填	为了区分不同的订单类型
             */
            response.setOrderType(item.getOrderType());
            /**
             * 活动 id	必填
             */
            response.setActivityId(item.getActivityId());
            /**
             * 活动项目 ID	必填
             */
            response.setActivityTermId(item.getCoverActivityTermRequest().getActivityTermId());
            /**
             * 参加结果	必填
             */
            response.setCoverResult(false);
            /**
             * 参加结果信息	必填	参加结果信息
             */
            response.setCoverResultMsg("未查询到活动使用记录，回补失败！");
            return response;
        }).collect(Collectors.toList());

        coverInActivityResponse.setCoverActivityResponseList(collect);
    }


    /**
     * 查询可用的活动项目
     *
     * @return
     */
    public ActivityInfoResponse queryCanUsingActivityTerm(ActivityInfoRequest request) {
        ActivityInfoResponse response = new ActivityInfoResponse();

        /**
         * 活动id 必填
         */
        response.setActivityId(titleEntity.obtainActivityId());

        //主订单id	必填
        response.setMainOrderId(request.getMainOrderId());

        //按组管控 20220811 add by myw
        response.setGroupControl(titleEntity.obtainControlByGroupFlag());
        //子单id 必填
        response.setSubOrderId(request.getSubOrderId());
//        /**
//         * 	形式说明	必填
//         * 	ZS01-买赠
//         * 	ZS02-搭赠
//         *    ZS03-特价
//         */
        response.setActivityFacadeCode(titleEntity.obtainFacadeCode());
//
//        /**
//         * 便签 Id
//         */
        response.setActivityNoteId(titleEntity.obtainActivityNoteId());
//        /**
//         * 便签名称	活动描述
//         */
        response.setActivityNoteName(titleEntity.obtainActivityNoteName());
//        /**
//         * 申请费用
//         */
        response.setApplyFee(titleEntity.hasTitleExt()?titleExtEntity.obtainApplyFee(): titleEntity.obtainApplyFee());
//        /**
//         * 货币
//         */
        response.setCurrency(titleEntity.obtainCurrency());
//
//        /**
//         * 订单开始时间
//         */
        response.setOrderStartDate(titleEntity.obtainOrderStartDate());
//        /**
//         * 	订单结束时间
//         */
//        private Date orderEndDate;
        response.setOrderEndDate(titleEntity.obtainOrderEndDate());
//
//        /**
//         * 活动状态
//         */
        response.setActivityStatus(titleEntity.obtainActivityStatus());
//        /***
//         * 活动明细项目结果
//         */
        response.setActivityTermList(convert2TermResponseList(request));
        /***
         * 是否禁止选择此活动(坎级搭赠同一主单下只能有一个明细行编辑坎级搭赠活动)
         *
         * */
//        response.setDisableChooseFlag(false);

        /**
         * 活动抬头扩展表的版本号
         */
        versionDeal(response,this);
        return response;
    }

    /**
     * 版本号处理
     *
     * @param response
     * @param activityEntity
     */
    private void versionDeal(ActivityInfoResponse response, ActivityEntity activityEntity) {
        if (Objects.nonNull(activityEntity) && "X".equals(activityEntity.getTitleEntity().obtainControlByGroupFlag())) {
            if (Objects.isNull(titleExtEntity) || Objects.isNull(titleExtEntity.obtainVersion())) {
                log.error("该活动抬头没有对应的活动抬头扩展数据" + JSONObject.toJSONString(activityEntity.getTitleEntity().getActivityTitleSDO()));
                throw new ProcessException("OTS-08-002-01-16-013");
            } else {
                    response.setActivityTitleExtVersion(titleExtEntity.obtainVersion());
            }
        }
    }

    /**
     * 转换成TermResponseList 列表
     *
     * @param request
     * @return
     */
    @ProcessInvoker(printLog = true)
    private List<ActivityTermResponse> convert2TermResponseList(ActivityInfoRequest request) {
        List<ActivityTermResponse> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(termEntityList)) {
//            ActivityTermResponse response = new ActivityTermResponse();
//            response.setFeatures("活动项目列表为空，无法正常使用");
//            result.add(response);
            log.error("活动项目列表为空，无法正常使用!");
            return result;
        }

        for (ActivityTermEntity term : termEntityList) {
            ActivityTermResponse termResponse = convert2TermResponse(term, request);
//            特价活动，特价不为空，添加到返回结果中
            if (ActivityTypeEnum.SPECIAL_OFFER_PRICE.getValue().equals(this.getTitleEntity().obtainFacadeCode())) {
                if (StringUtils.isNotBlank(termResponse.getSpecialOfferPrice())
                        && (BigDecimal.ZERO.compareTo(new BigDecimal(termResponse.getSpecialOfferPrice())) != 0)) {
                    result.add(termResponse);
                    continue;
                }
            }

//            买赠或者搭赠活动，赠品列表不为空，添加到返回结果中
            if (ActivityTypeEnum.BUY_GIFT.getValue().equals(this.getTitleEntity().obtainFacadeCode())
                    || ActivityTypeEnum.TAKE_GIFT.getValue().equals(this.getTitleEntity().obtainFacadeCode())) {
                if (CollectionUtils.isNotEmpty(termResponse.getGiftList())) {
                    result.add(termResponse);
                }
            }

            //补货类型活动,直接添加导返回结果中
            if(replenishList.contains(this.getTitleEntity().obtainFacadeCode())){
                result.add(termResponse);
            }

        }
        return result;
    }

    /**
     * ActivityTermEntity 转换 ActivityTermResponse
     *
     * @param term
     * @param request
     * @return
     */
    private ActivityTermResponse convert2TermResponse(ActivityTermEntity term, ActivityInfoRequest request) {
        ActivityTermResponse termResponse = new ActivityTermResponse();
        /**
         * 	活动项目 id
         */
        termResponse.setActivityTermId(term.obtainActivityTermId());

        /**
         * 	业态	，必填
         */
        termResponse.setBusinessFormat(term.obtainBusinessFormat());
        /**
         * 	品牌	，必填
         */
        termResponse.setBrand(term.obtainBrand());
        /**
         * 品类	，必填
         */
        termResponse.setItemCategory(term.obtainItemCategory());
        /**
         * 品项	，必填
         */
        termResponse.setItemSubject(term.obtainItemSubject());
        /**
         * 特价	，必填
         */
        termResponse.setSpecialOfferPrice(term.obtainSpecialOfferPrice(this.titleEntity.obtainFacadeCode(), request));

        /**
         * 形式描述	，必填
         */
        termResponse.setFacadeDescription(term.obtainFacadeDescription());

        /**
         * 活动力度说明 必填
         */
        termResponse.setIntensityOfActivityDesc(term.obtainIntensityOfActivityDesc());

        /**
         * 申请费用	必填
         */
        termResponse.setApplyFee(term.obtainApplyFee());
        /**
         * 申请原品数量	Y
         */
        termResponse.setApplyItemQuantity(term.obtainApplyItemQuantity());
        /**
         * 活动发生数量
         */
        termResponse.setActivityOccupyQuantity(term.obtainActivityOccupyQuantity());

        /**
         * 活动发生费用
         */
        termResponse.setActivityOccupyFee(term.obtainActivityOccupyFee());
        /**
         * 活动剩余数量	Y
         */
        termResponse.setActivityRemainingQuantity(term.obtainActivityRemainingQuantity());

        /**
         * 活动剩余费用	Y
         */
        termResponse.setActivityRemainingFee(term.obtainActivityRemainingFee());

        /**
         * 销售单位
         */
        termResponse.setSalesUnit(term.obtainSalesUnit());
        /**
         * 更新版本号
         */
        termResponse.setVersion(term.obtainVersion());

        /**
         * 原品信息
         */
        termResponse.setItemId(term.obtainItemId());

        /**
         * 这里这样定义主要是为了和数据库保持一致，之后会做调整
         */
        termResponse.setGiftList(term.obtainGiftList(this.titleEntity.obtainFacadeCode(), request));
        /**
         * 根据赠品是否同单位来判断是否固定赠品（abandoned）
         *
         * */
//        if (CollectionUtils.isNotEmpty(termResponse.getGiftList())){
//            int size = termResponse.getGiftList().stream().collect(Collectors.groupingBy(ActivityItemInfo::getSalesUnit)).keySet().size();
//            termResponse.setIsFixedFlag(size >1);
//        }

        /**
         * 公用组标识
         */
        termResponse.setCommonGroupFlag(term.getCommonGroupFlag());

        /**
         * 赠品类型
         */
        termResponse.setGiftActivityType(term.obtainGiftType());
        /**
         * 参与满量赠的子单ID集合
         * */
        termResponse.setJoinFullBuySubIdList(term.getJoinFullBuySubIdList());

        return termResponse;
    }

    public void checkActivity(ParticipateActivityInfoRequest activityInfo, CheckActivityInfoResponse response) {
        response.setMainOrderId(activityInfo.getMainOrderId());
        response.setSubOrderId(activityInfo.getSubOrderId());
        response.setActivityId(activityInfo.getActivityId());

        //补货订单，不进行校验，直接返回
        if (ActivityOrderTagEnum.REPLENISH_ORDER.getCode().equals(activityInfo.getOrderType())) {
            response.setCheckResult(true);
            return;
        }

        //1. 校验参加活动抬头的下单时间
        //2. 校验参加活动的交货时间
        titleEntity.checkActivity(activityInfo, response);
        //如果校验不通过，直接返回
        if (!response.getCheckResult()) {
            return;
        }

        //如果活动项目ID 不匹配，就不会存在 term列表，这里不再进行校验
        ActivityTermEntity termEntity = termEntityList.stream().filter(
                e -> activityInfo.getParticipateActivityTermRequest().getActivityTermId()
                        .equals(e.obtainActivityTermId())).findFirst().orElse(null);
//        termEntity为空，直接返回
        if (Objects.isNull(termEntity)) {
            response.setCheckResult(false);
            return;
        }
        response.setActivityTermId(termEntity.obtainActivityTermId());

        //3. 校验活动的价格
        //4. 校验活动的赠品
        //todo qjg，这个地方是校验不住地，需要重新查询特价和搭赠和满赠活动
//        termEntity.checkActivity(activityInfo, response);

    }

    /**
     * 更新活动的数量和费用
     *
     * @param activityInfo
     * @return
     */
    @FunctionInvoker(printLog = true)
    public ParticipateActivityInfoResponse updateActivityQuantityAndFee(ParticipateActivityInfoRequest activityInfo) {
        ParticipateActivityInfoResponse response = new ParticipateActivityInfoResponse();

//        主单号
        response.setMainOrderId(activityInfo.getMainOrderId());
        /**
         * 订单类型	必填	为了区分不同的订单类型
         */
        response.setOrderType(activityInfo.getOrderType());
        /**
         * 活动 id	必填
         */
        response.setActivityId(activityInfo.getActivityId());
        /**
         * 活动项目 ID	必填
         */
        response.setActivityTermId(activityInfo.getParticipateActivityTermRequest().getActivityTermId());

        /**
         * 活动类型
         */
        response.setActivityType(titleEntity.obtainActivityType());
        /**
         * 子单号
         */
        response.setSubOrderId(activityInfo.getSubOrderId());

        /**
         * 物料编码
         */
        ActivityItemInfo item = activityInfo.getParticipateActivityTermRequest().getItem();
        response.setItemId(item.getItemId());


//        1.查询是否存在活动使用记录，不存在则进行活动的占用和活动记录数据的写入
//        2. 修改活动项目表中的占用的数量和金额
//        3. 组织返回结果
        Boolean isParticipated = this.useEntity.isRepeatUsedActivity();
        if (!isParticipated) {

            Long[] participateSuccessData = new Long[2];
            //数组 0 的位置保存本次预占成功数量
            participateSuccessData[0] = 0L;
            //数组 1 的位置保存本次预占用金额
            participateSuccessData[1] = 0L;

            if (CollectionUtils.isEmpty(this.termEntityList)) {
                log.error("没有查到活动项目，原品 ID=" + item.getItemId() + " 与活动Id=" + activityInfo.getActivityId()
                        + "，活动项目id=" + activityInfo.getParticipateActivityTermRequest().getActivityTermId()
                        + "不匹配！");

                response.setParticipateResult(false);
                response.setParticipateResultMsg("当前活动项目未命中，请分析后重试！");
                response.setParticipateSuccessItemCount(0);
                response.setParticipateSuccessFee(0L);
                return response;
            }

            //是否需要计算赠品费用 坎级搭赠部分子单不需要计算占用费用 true不计算
            boolean noNeedCalcOccupyFee=false;
            if (MapUtils.isNotEmpty(activityInfo.getFeatures())){
                 noNeedCalcOccupyFee = "true".equals(activityInfo.getFeatures().getOrDefault(ACTIVITY_GIFT_HAVE_FEE, "false"));
            }

            Boolean participateResult = this.termEntityList.get(0).judgeOccupyQuantityAndFee(
                    activityInfo.getParticipateActivityTermRequest(), this.titleEntity.obtainFacadeCode(), participateSuccessData,noNeedCalcOccupyFee);
            //参加结果	必填
            response.setParticipateResult(participateResult);

            //占用成功的数量
            response.setParticipateSuccessItemCount(participateSuccessData[0].intValue());
            //占用成功的金额
            response.setParticipateSuccessFee(participateSuccessData[1]);

            /**
             * 参加结果信息	必填	参加结果信息
             */
            //分组管控和非分组管控分别处理
            if (Objects.nonNull(this.titleEntity) && "X".equals(this.titleEntity.obtainControlByGroupFlag())) {
                response.setParticipateResultMsg(participateResult ? "预占活动成功！" :
                        "活动剩余量或者活动金额不足，订单上无法使用该活动，生成销售订单失败，请修改预计回复数量！活动便签号="
                                + titleEntity.obtainActivityNoteId());
            } else {
                response.setParticipateResultMsg(participateResult ? "预占活动成功！" :
                        "活动剩余量或者活动金额不足，订单上无法使用该活动，生成销售订单失败，请修改预计回复数量！活动 Id="
                                + titleEntity.obtainActivityId() + ",活动项目 id=" + termEntityList.get(0).obtainActivityTermId());
            }

            log.info("参加活动结果 = {}", response);

        } else {
            /**
             * 参加结果	必填
             */
            response.setParticipateResult(false);
            //参加成功的活动数量
            response.setParticipateSuccessItemCount(0);
            response.setParticipateSuccessFee(0L);
            /**
             * 参加结果信息	必填	参加结果信息
             */
            response.setParticipateResultMsg("参加活动失败,当前订单已存在活动使用记录!");
            log.error("参加活动失败,当前入参=" + JSON.toJSONString(activityInfo) + ",订单已存在活动使用记录:" + JSON.toJSONString(this.useEntity.getActivityUseRecordRelationSDO()));
        }


        return response;
    }

    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    /**
     * 更新活动的数量和费用_活动回补
     *
     * @param activityInfo
     * @return
     */
    @FunctionInvoker
    public CoverActivityInfoResponse updateActivityQuantityAndFeeForCover(CoverActivityInfoRequest activityInfo) {

        //活动占用量和金额取反表示扣减回去
//        ActivityItemInfo item = activityInfo.getCoverActivityTermRequest().getItem();
//        item.setItemQuantity(item.getItemQuantity() * -1);
//        activityInfo.getCoverActivityTermRequest().setItem(item);
//
        CoverActivityInfoResponse response = new CoverActivityInfoResponse();
        response.setActivityId(activityInfo.getActivityId());

//        主单号
        response.setMainOrderId(activityInfo.getMainOrderId());

        /**
         * 子单号
         */
        response.setSubOrderId(activityInfo.getSubOrderId());

        /**
         * 物料编码
         */
        response.setItemId(activityInfo.getCoverActivityTermRequest().getItem().getItemId());
        /**
         * 订单类型	必填	为了区分不同的订单类型
         */
        response.setOrderType(activityInfo.getOrderType());
        /**
         * 活动 id	必填
         */
        response.setActivityId(activityInfo.getActivityId());
        /**
         * 活动项目 ID	必填
         */
        response.setActivityTermId(activityInfo.getCoverActivityTermRequest().getActivityTermId());

        //  1.查询是否存在活动使用记录，存在则进行活动占用量和占用金额进行回补操作
        //  2. 修改活动项目表中的占用的数量和金额
        //  3. 组织返回结果
        Boolean coverResult = this.useEntity.getActivityUseRecordRelationSDO() != null;

        if (coverResult) {

            //获取活动使用记录features
            String features = this.useEntity.obtainFeatures();

            coverResult = this.termEntityList.get(0).updateOccupyQuantityAndFeeForCover(
                    activityInfo.getCoverActivityTermRequest(), this);
            if ("X".equals(JSONObject.parseObject(features).get("groupControlFlag"))) {
                coverResult = this.titleExtEntity.updateOccupyQuantityAndFeeForCover(this);
            }


            response.setCoverResult(coverResult);
            log.info("回补活动结果 = {}", response);
//            回补结果信息
            response.setCoverResultMsg(coverResult ? "活动回补成功！" : "活动回补失败！");
        } else {
            /**
             * 参加结果	必填
             */
            response.setCoverResult(false);

            log.error("活动回补失败,当前订单不存在活动使用记录 = {}", activityInfo);
            response.setCoverResultMsg("活动回补失败,当前订单不存在活动使用记录");
        }

        return response;
    }



    @FunctionInvoker
    public Boolean saveActivityTerm() {
        if (CollectionUtils.isEmpty(this.termEntityList)) {
            log.error("termEntityList 为空，保存失败！");
            return false;
        }
        //完成单表的数据保存
        ActivityTermEntity termEntity = this.termEntityList.get(0);
        return termEntity.save();
    }

    @FunctionInvoker
    public void saveActivityUseRecord(ParticipateActivityInfoRequest activityInfo, ParticipateActivityInfoResponse infoResponse,Boolean groupControlFlag) {
        if (CollectionUtils.isEmpty(this.termEntityList)) {
            return;
        }

        //完成单表的数据保存
        ActivityTermEntity termEntity = this.termEntityList.get(0);
        //完成活动关系表数据的插入
        ActivityItemInfo item = activityInfo.getParticipateActivityTermRequest().getItem();
        ActivityUseRecordSDO sdo = ActivityUseRecordSDO.builder()
                //活动 id
                .activityId(this.titleEntity.obtainActivityId())
                //活动项目 id
                .activityTermId(termEntity.obtainActivityTermId())
                //主单号
                .mainOrderId(activityInfo.getMainOrderId())
                //子单号
                .subOrderId(activityInfo.getSubOrderId())
                .orderType(activityInfo.getOrderType())
                //货品数量
                .itemQuantity(Long.valueOf(infoResponse.getParticipateSuccessItemCount()))
                //活动占用费用
                .activityOccupyFee(infoResponse.getParticipateSuccessFee())
                //货品 ID
                .itemId(item.getItemId())
                //销售单位
                .salesUnit(item.getSalesUnit())
                //库存单位
                .stockUnit(item.getStockUnit())
                //转换单位
                .conversionUnit(item.getConversionUnit())
                //货品名称
                .itemName(item.getItemName())
                //资源占用序列号
                .sequenceNo(activityInfo.getSequenceNo())
                .build();

        //不为空,存储serialNumber，预报单号forecastOrderBizId
        if (MapUtils.isNotEmpty(activityInfo.getFeatures())) {
            Map<String, String> map = new HashMap<>();
            if (StringUtils.isNotBlank(sdo.getFeatures())) {
                map = JSON.parseObject(sdo.getFeatures(), Map.class);
            }
            map.putAll(activityInfo.getFeatures());
            map.put(ACTIVITY_TERM_SNAPSHOT, termEntity.getSnapshot());
            if(groupControlFlag){
                map.put("groupControlFlag","X");
            }
            String features = JSON.toJSONString(map);
            sdo.setFeatures(features);
        }

        //执行更新活动占用量和占用金额成功时才写入数据到活动使用记录表
        Boolean result = writeRepository.createActivityUseRecord(sdo);
        infoResponse.setParticipateResult(result);
        infoResponse.setActivityType(this.obtainActivityType());
        infoResponse.setParticipateResultMsg(result ? "参加活动成功!" : "参加活动失败!");
        this.titleExtEntity.save();
    }

    /**
     * 活动回补，回补
     *  @param
     *
     */
    public Boolean cover() {
        if (CollectionUtils.isEmpty(this.termEntityList)) {
            return false;
        }

        //修改活动占用量和占用金额
        String features = useEntity.obtainFeatures();
        Boolean updateTermResult;
        updateTermResult = this.termEntityList.get(0).saveForVersion();
        if (updateTermResult&&"X".equals(JSONObject.parseObject(features).get("groupControlFlag"))) {
            updateTermResult = this.titleExtEntity.saveForVersion();
        }

        //这里是为了测试事务是否生效的语句。todo qjg 上线前要删除
        //Long test = 10/CrPriceCenterConstants.TEST_TRANSACTIONAL_ACTIVITY_COVER();

        //执行回补活动占用量和占用金额成功时逻辑删除活动使用记录
        if (updateTermResult) {
            //逻辑删除活动使用记录数据
            Boolean deleteResult = useEntity.deleteActivityUseRecord();
            if (!deleteResult) {
                throw new FacadeException("OTS-08-002-01-16-006");
            }
            return true;
        }

        return false;
    }

    /**
     * 获取活动类型
     *
     * @return 活动类型
     */
    public String obtainActivityType() {
        return this.titleEntity.obtainFacadeCode();
    }

    public Boolean saveActivityTitleExt() {
        if (Objects.isNull(this.titleExtEntity)) {
            log.error("titleExtEntity 为空，保存失败！");
            return false;
        }
        //完成单表的数据保存
        return this.titleExtEntity.updateOccupyQuantityAndFee();
    }
}
